package com.dc.chat.controller;

import com.dc.chat.app.biz.UserBiz;
import com.dc.chat.entity.to.ResponseBean;
import com.dc.chat.entity.to.user.GetInfoResp;
import com.dc.chat.entity.to.user.SearchUserReq;
import com.dc.chat.entity.to.user.SearchUserResp;
import com.dc.chat.entity.to.user.UserNameAndPasswordBo;
import com.dc.chat.interceptor.security.Security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserBiz userBiz;
    @PostMapping("/login")
    public ResponseBean<String> login(@Validated @RequestBody UserNameAndPasswordBo req){
        return userBiz.login(req.getUsername(),req.getPassword());
    }
    @PostMapping("/register")
    public ResponseBean<Integer> register(@Validated @RequestBody UserNameAndPasswordBo req){
        return userBiz.register(req.getUsername(),req.getPassword());
    }
    @Security
    @PostMapping("/getInfo")
    public ResponseBean<GetInfoResp> getInfo(){
        return userBiz.getInfo();
    }
    @Security
    @PostMapping("/searchUser")
    public ResponseBean<SearchUserResp> searchUser(@Validated @RequestBody SearchUserReq req){
        String username = req.getUsername();
        return userBiz.searchUser(username);
    }
    @Security
    @PostMapping("/editNickname")
    public ResponseBean editNickname(@RequestBody Map req){
        String nickname = (String) req.get("nickname");
        System.out.println(nickname);
        return userBiz.editNickname(nickname);
    }
    @Security
    @PostMapping("/changeAvatar")
    public ResponseBean changeAvatar(@RequestParam("file")MultipartFile file) throws IOException {
        return userBiz.changeAvatar(file);
    }
}
